package com.movie.playvideos;

import io.vov.vitamio.LibsChecker;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.actionbarsherlock.app.ActionBar;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.movie.playvideos.common.ApplicationData;
import com.movie.playvideos.common.ConnectToServer;
import com.movie.playvideos.common.ConnectToServer.GetAuthenListener;
import com.movie.playvideos.common.ConnectToServer.GetListVideoLiveListener;
import com.movie.playvideos.common.ConnectToServer.GetTokenListener;
import com.movie.playvideos.common.DecoderJson;
import com.movie.playvideos.common.Setting;
import com.movie.playvideos.common.StartActivityCommon;
import com.movie.playvideos.fragment.MenuFragment;
import com.movie.playvideos.fragment.MenuFragment.ListSelectionListener;
import com.movie.playvideos.fragment.MovieThuyNgaFragment;
import com.movie.playvideos.objects.MediaItemObject;
import com.movie.playvideos.objects.MediaObject;


public class MainActivity extends SlidingFragmentActivity implements ListSelectionListener, GetTokenListener, GetListVideoLiveListener, GetAuthenListener {
	
	private MenuFragment mFrag;
	private String token;
	private ApplicationData gData;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!LibsChecker.checkVitamioLibs(this))
			return;
		
		// add top banner
		com.actionbarsherlock.app.ActionBar.LayoutParams lp = new com.actionbarsherlock.app.ActionBar.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		
		View title = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
				.inflate(R.layout.home_header, null);
		
		getSupportActionBar().setCustomView(title, lp);
		getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		
		setContentView(R.layout.content_frame);
		
		// set the Behind View
		setBehindContentView(R.layout.menu_frame);
		if (savedInstanceState == null) {
			FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
			mFrag = new MenuFragment();
			t.replace(R.id.menu_frame, mFrag);
			t.commit();
		} else {
			mFrag = (MenuFragment) this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
		}

		// customize the SlidingMenu
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBackgroundResource(R.drawable.bg_slidemenu);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setDisplayUseLogoEnabled(false);
		
		Log.e("Thinhdt", "getToken");
		// bat dau get Token 
		ConnectToServer.getToken(this, this);
	}
	
	@Override
	public void onListSelection(int position) {
		Log.d("Thinhdt", "Main Activity onListSelection");
		
		switch (position) {
		case 0:
			Fragment movies = new MovieThuyNgaFragment();
			
			FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
			ft.add(R.id.content_frame, movies);
			ft.addToBackStack(null);
			ft.commit();
			
			break;

		default:
			break;
		} 
		// close slidemenu
		toggle();
	}

	@Override
	public void getVideoSuccess(MediaObject media) {
		if(media.getListMedia() == null){
			//Log.e("Thinhdt", "listMedia: " + media.getListMedia().size() + "");
		}
		else{
			MediaItemObject mediaOb = media.getListMedia().get(0);
			if(Setting.Debug){
				Log.e("VideoUrl", mediaOb.getPlayVideoUrl());
				String path = mediaOb.getPlayVideoUrl();
				//path = "http://hn.m.24h.com.vn/upload_mobile/1-2014/videoclip_hd/2014-03-26/1395783689-ManUtd_ManCity_Mp4_Cao_0000000000001.mp4";
				StartActivityCommon.startActivityPlayVideo(path, this);
			}
			
		}
			
	}

	@Override
	public void getVideoFailure() {
	}

	@Override
	public void getTokenSuccess(String data) {
		token = DecoderJson.getTokenFromData(data);
		
		Log.e("Thinhdt", "getToken: " + token);
		// bat dau authen
		gData = (ApplicationData)getApplicationContext();
		Log.d("Thinhdt", "username: " + gData.getUsername());
		Log.d("Thinhdt", "password: " + gData.getPassword());
		ConnectToServer.getAuthen(MainActivity.this, this, gData.getUsername(), gData.getPassword(), token);		
	}

	@Override
	public void getTokenFailure() {
		
	}

	@Override
	public void getAuthenSuccess(String data) {
		Log.e("Thinhdt", "Authen Data: "+ data); //data == rong~
		
		try {
			JSONObject json = new JSONObject(data);
			int errorCode = json.getInt("error_code");
			if(errorCode == 401){
				String newToken = json.getJSONObject("data").getString("Token");
				this.token = newToken;
				gData = (ApplicationData)getApplicationContext();
				Log.d("Thinhdt", "username: " + gData.getUsername());
				Log.d("Thinhdt", "password: " + gData.getPassword());
				
				ConnectToServer.getAuthen(MainActivity.this, this, gData.getUsername(), gData.getPassword(), newToken);		
			}
			else{
				ConnectToServer.getListLiveTv(this, token, this);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void getAuthenFailure() {
		
	}
	
	public static void actionMainActivity(Activity fromActivity) {
		Intent intent = new Intent(fromActivity, MainActivity.class);
		fromActivity.startActivity(intent);
	}
	
}
